package it.gameplanet.model;

import java.io.Serializable;

@SuppressWarnings("serial")
public final class User implements Serializable, Comparable<User> {

	private String email;
	private String authDomain;
	private boolean administrator;
	
	@SuppressWarnings("unused")
	private User() {
	}

	public User(String email, String authDomain, boolean isAdmin) {
		if (email == null)
			throw new NullPointerException("email must be specified");
		if (authDomain == null) {
			throw new NullPointerException("authDomain must be specified");
		} else {
			this.email = email;
			this.authDomain = authDomain;
			this.administrator = isAdmin;
		}
	}

	public String getNickname() {
		int indexOfDomain = email.indexOf((new StringBuilder()).append("@")
				.append(authDomain).toString());
		if (indexOfDomain == -1)
			return email;
		else
			return email.substring(0, indexOfDomain);
	}

	public String getAuthDomain() {
		return authDomain;
	}

	public String getEmail() {
		return email;
	}

	public boolean isAdministrator() {
		return administrator;
	}

	public String toString() {
		return email;
	}

	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		} else {
			User user = (User) object;
			return user.email.equals(email)
					&& user.authDomain.equals(authDomain);
		}
	}

	public int hashCode() {
		return 17 * email.hashCode() + authDomain.hashCode();
	}

	public int compareTo(User user) {
		return email.compareTo(user.email);
	}
}
